// pages/search/search.js
const db = wx.cloud.database()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    value1: 'asc',
    option1: [
      { text: '价格升序', value: 'asc' },
      { text: '价格降序', value: 'desc' },
    ]
  },
  page: 1,
  value: '',
  order: 'asc',

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.fetchList()
  },

  fetchList(value) {

    db.collection('mock_product').skip((this.page - 1) * 20).limit(20).orderBy('price',this.order).where({
      title: db.RegExp({
        regexp: this.value,
        options: 'i',
      })
    }).get().then(res => {
      this.setData({
        list: (this.page === 1 ? [] :this.data.list).concat(res.data)
      })
    })
  },
  changeOrder(e) {
    this.order = e.detail
    this.page = 1;
   this.fetchList()
  },
  onSearch(e) {
    this.value = e.detail
    this.fetchList()
  },
  onReachBottom() {
    this.page+=1;
    this.fetchList()
  }
})